//
//  SinaStatusCell.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/17.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
import SDWebImage

private let margin : CGFloat = 8
//配图视图之间的间距
private let imageMargin: CGFloat = 2
//最大宽度
private let maxWidth = ScreenWidth - 2 * margin
//每个图片的宽度(如果一行显示三张图片)
private let itemWidth = (maxWidth - 2 * imageMargin) / 3

class SinaStatusCell: UITableViewCell {
    
    //用户头像
    @IBOutlet weak var iconView: UIImageView!
    //用户名称
    @IBOutlet weak var nameLbl: UILabel!
    //用户等级
    @IBOutlet weak var mbRankView: UIImageView!
    //微博时间
    @IBOutlet weak var timeLbe: UILabel!
    
    //微博来源
    @IBOutlet weak var sourcelbl: UILabel!
    //微博正文
    @IBOutlet weak var contentlbl: UILabel!
    //微博用户认证类型
    @IBOutlet weak var avatarView: UIImageView!
    //配图视图
    @IBOutlet weak var pictureView: SinaPictureView!
    //配图视图宽约束
    @IBOutlet weak var PictureViewW: NSLayoutConstraint!
    //配图视图高约束
    @IBOutlet weak var pictuViewH: NSLayoutConstraint!
    //配图视图布局约束
    @IBOutlet weak var PictureViewFlowLayout: UICollectionViewFlowLayout!
    //配图视图顶部约束
    @IBOutlet weak var pictureViewTop: NSLayoutConstraint!
    //转发微博lbl
    @IBOutlet weak var retweetedLbl: UILabel!
    //工具条
    
    @IBOutlet weak var toolBar: UIView!
    //cell需要绑定数据
    var viewmodel :SinaStatusViewModel?{
        
        didSet{
            
            //            //设置子控件的数据
            iconView.sd_setImage(with: viewmodel?.iconURL)
            nameLbl.text = viewmodel?.status?.user?.name
            //            timeLbe.text = viewmodel?.status?.create_at
            timeLbe.text = viewmodel?.timeText
            //            sourcelbl.text = viewmodel?.status?.source
            sourcelbl.text = viewmodel?.sourceText
            contentlbl.text = viewmodel?.status?.text
            avatarView.image = viewmodel?.verifitedImage
            mbRankView.image = viewmodel?.mbRankImage
            //let count = viewmodel?.status?.pic_urls?.count ?? 0
            //需要修改count获取的方式
            let count = viewmodel?.imageURLs?.count ?? 0
            //let pSize = calculateWandHConstriant(count: count)
            //根据约束对象的约束值
            //PictureViewW.constant = pSize.width
            //pictuViewH.constant = pSize.height
            let  result = calculateWandHConstriant(count: count)
            PictureViewW.constant = result.pSize.width
            pictuViewH.constant = result.pSize.height
            //还需要更新itemSize,会出现复用的问题
            PictureViewFlowLayout.itemSize = result.itemSize
            //根据count值,修改顶部约束(避免没有图片的微博,有两个间距)
            pictureViewTop?.constant = (count == 0 ? 0 : margin)
            //给配图视图设置数据源
            //print(viewmodel?.status?.pic_urls)
            //pictureView.pictureInfos = viewmodel?.status?.pic_urls
            //转发微博和原创微博都是公用了配图视图
            pictureView.pictureInfos = viewmodel?.imageURLs
            //设置配图视图的文本
            retweetedLbl?.text = viewmodel?.status?.retweeted_status?.text
            //            //8.1不会立即更新约束,配图视图的大小还是使用的上一个cell的尺寸
            //            pictureView.layoutIfNeeded()
            ////            print("计算后的尺寸\(pSize)")
            ////            print(pictureView.bounds)
            //print("^^^计算后的尺寸\(pSize)")
            //            print("^^^配图视图大小\(pictureView.bounds)")
            pictureView.layoutIfNeeded()
            
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置背景色
        //self.contentView.backgroundColor = randomColor()
        //设置行数为 0
        self.contentlbl.numberOfLines = 0
        //设置最大的布局宽度
        //print(self.bounds)
        self.contentlbl.preferredMaxLayoutWidth = ScreenWidth  - 2 *  margin
        self.retweetedLbl?.numberOfLines = 0
        //设置最大的布局宽度
        self.retweetedLbl?.preferredMaxLayoutWidth = ScreenWidth - 2*margin
        //设置流水布局
        PictureViewFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置行和列最小间距
        PictureViewFlowLayout.minimumLineSpacing = imageMargin
        PictureViewFlowLayout.minimumInteritemSpacing = imageMargin
        //设置选中的样式
        //selectionStyle = .none
    }
    //MARK: -
    //MARK: - 根据个数计算图片的宽高约束值
    //元组类型:可以使用多个类型组合成一个类型
    //private func calculateWandHConstriant(count:Int) ->CGSize {
    private func calculateWandHConstriant(count:Int) ->(pSize: CGSize,itemSize:CGSize){
        
        
        
        if count == 0 {
            //            return CGSize.zero
            return (CGSize(width:1,height:1),CGSize(width:1,height:1))
        }
        if count == 1{
            //计算图片的大小  获取图片  -> image.size  必须确保在这里一定能够获取到图片
            //不能够在这个地方下载图片 否则会执行多次刷新(有多少个单张图片就需要刷新多少次)
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewmodel?.imageURLs?.last?.wap360 ?? "") {
                let imageSize = image.size
                //不能再if中单独设置itemSize
                //pictureViewFlowLayout.itemSize = imageSize
                return (imageSize,imageSize)
            }
            
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
            
        }
        //判断四张图片
        if count == 4 {
            let width = 2 * itemWidth + imageMargin
            return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))
            
        }
        
        //其他的多张图片 需要确定行数
        /*
         1,2,3  1
         4,5,6  2
         7,8,9  3
         */
        //已知列数和总个数 需要确定行数
        let row = CGFloat((count - 1) / 3) + 1
        let h = row * itemWidth + (row - 1) * imageMargin
        return (CGSize(width: maxWidth, height: h),CGSize(width: itemWidth, height: itemWidth))
        
        
    }
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
